﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using XCharts;

namespace KFGame
{
    [System.Serializable]
    public class ChartModule
    {
        [SerializeField]
        private string m_Name;

        [SerializeField]
        private string m_Title;

        [SerializeField]
        private bool m_Selected;
        [SerializeField]
        private GameObject m_Panel;

        public string name
        {
            get
            {
                return m_Name;
            }
            set
            {
                m_Name = value;
            }
        }

        public string title
        {
            get
            {
                return m_Title;
            }
            set
            {
                m_Title = value;
            }
        }

        public bool select
        {
            get
            {
                return m_Selected;
            }
            set
            {
                m_Selected = value;
            }
        }

        public GameObject panel
        {
            get
            {
                return m_Panel;
            }
            set
            {
                m_Panel = value;
            }
        }

        public Button button { get; set; }
    }

    [DisallowMultipleComponent]
    [ExecuteInEditMode]
    public class KFStatitcsDataCharts : MonoBehaviour
    {
        [SerializeField]
        private Color m_ButtonNormalColor;

        [SerializeField]
        private Color m_ButtonSelectedColor;

        [SerializeField]
        private Color m_ButtonHighlightColor;

        [SerializeField]
        private List<ChartModule> m_ChartModule = new List<ChartModule>();

        private GameObject m_BtnClone;

        private Theme m_SelectedTheme;

        private int m_LastSelectedModuleIndex;

        private Text m_Title;

        private ScrollRect m_ScrollRect;

        private Mask m_Mark;



        public int getChartCount()
        {
            return m_ChartModule.Count;
        }
        /// <summary>
        /// 初始化工作
        /// </summary>
        void Awake()
        {
            m_SelectedTheme = Theme.Default;

            m_ButtonNormalColor = ChartHelper.GetColor("#293C55FF");
            m_ButtonSelectedColor = ChartHelper.GetColor("#e43c59ff");
            m_ButtonHighlightColor = ChartHelper.GetColor("#0E151FFF");

            m_ScrollRect =
                transform.Find("chart_detail").GetComponent<ScrollRect>();
            m_Mark =
                transform.Find("chart_detail/Viewport").GetComponent<Mask>();
            m_Mark.enabled = true;
            m_Title = transform.Find("chart_title/Text").GetComponent<Text>();

            InitThemeButton();
            InitModuleButton();
        }


        void ResetParam()
        {
            var charts = transform.GetComponentsInChildren<BaseChart>();
            foreach (var chart in charts)
            {
                chart.legend.itemWidth = 20f;
                chart.legend.itemHeight = 10f;
                chart.legend.textStyle.fontSize = 16;
            }
        }

        void Update()
        {
            EditorDatas();
        }

        /// <summary>
        ///在开发编辑状态下
        ///  编辑数据更新内容
        /// </summary>
        void EditorDatas()
        {
#if UNITY_EDITOR
            if (m_ChartModule.Count <= 0) return;
            int selectedModuleIndex = -1;
            for (int i = 0; i < m_ChartModule.Count; i++)
            {
                if (selectedModuleIndex >= 0 && i > selectedModuleIndex)
                {
                    m_ChartModule[i].select = false;
                }
                else if (m_ChartModule[i].select)
                {
                    selectedModuleIndex = i;
                }
            }
            if (selectedModuleIndex < 0) selectedModuleIndex = 0;
            if (selectedModuleIndex != m_LastSelectedModuleIndex)
            {
                InitModuleButton();
            }
            if (!Application.isPlaying) m_Mark.enabled = false;
#endif
        }

        /// <summary>
        /// 初始化按钮列表
        /// </summary>
        void InitModuleButton()
        {
            var btnPanel = transform.Find("chart_list");
            m_BtnClone = transform.Find("btn_clone").gameObject;
            m_BtnClone.SetActive(false);
            ChartHelper.HideAllObject(btnPanel);

            foreach (var module in m_ChartModule)
            {
                var btnName = "btn_" + module.name;
                GameObject btn;
                if (btnPanel.Find(btnName))
                {
                    btn = btnPanel.Find(btnName).gameObject;
                    btn.name = btnName;
                    ChartHelper.SetActive(btn, true);
                }
                else
                {
                    btn = GameObject.Instantiate(m_BtnClone);
                    btn.SetActive(true);
                    btn.name = btnName;
                    btn.transform.SetParent(btnPanel);
                    btn.transform.localPosition = Vector3.zero;
                }
                btn.transform.localScale = Vector3.one;
                module.button = btn.GetComponent<Button>();
                module.button.GetComponentInChildren<Text>().text = module.name;

                ChartHelper
                    .AddEventListener(btn.gameObject,
                    EventTriggerType.PointerDown,
                    (data) =>
                    {
                        ClickModule(module);
                    });
            }

            for (int i = 0; i < m_ChartModule.Count; i++)
            {
                var module = m_ChartModule[i];
                if (module.select)
                {
                    ClickModule(module);
                    m_LastSelectedModuleIndex = i;
                    break;
                }
            }
        }

        /// <summary>
        /// 通过项号选择
        /// </summary>
        /// <param name="index"></param>
        public void select(int index)
        {
            if (index < 0)
            {
                closeSelce();
                return;
            }


            if (index >= m_ChartModule.Count)
                return;

            ClickModule(m_ChartModule[index]);

        }

        void closeSelce()
        {
            foreach (var module in m_ChartModule)
            {

                var block = module.button.colors;
                block.highlightedColor = m_ButtonHighlightColor;
                block.normalColor = m_ButtonNormalColor;
                module.button.colors = block;
                module.panel.SetActive(false);
                module.select = false;

            }
        }

        /// <summary>
        /// 点击按钮 选择模块
        /// </summary>
        /// <param name="selectedModule"></param>
        void ClickModule(ChartModule selectedModule)
        {
            foreach (var module in m_ChartModule)
            {
                if (selectedModule != module)
                {
                    var block = module.button.colors;
                    block.highlightedColor = m_ButtonHighlightColor;
                    block.normalColor = m_ButtonNormalColor;
                    module.button.colors = block;
                    module.panel.SetActive(false);
                    module.select = false;
                }
                else
                {
                    var block = module.button.colors;
                    block.highlightedColor = m_ButtonSelectedColor;
                    block.normalColor = m_ButtonSelectedColor;
                    module.button.colors = block;
                    module.panel.SetActive(true);
                    module.select = true;
                }
            }
            m_ScrollRect.content =
                selectedModule.panel.GetComponent<RectTransform>();
            m_Title.text =
                string.IsNullOrEmpty(selectedModule.title)
                    ? selectedModule.name
                    : selectedModule.title;
        }


        /// <summary>
        /// 初始化皮肤
        /// </summary>
        void InitThemeButton()
        {
            // SelecteTheme(Theme.Default);
            SelecteTheme(Theme.Light);

            // SelecteTheme(Theme.Dark);

            //SelecteTheme(Theme.Default);
        }

        /// <summary>
        /// 选择皮肤
        /// </summary>
        /// <param name="theme"></param>
        void SelecteTheme(Theme theme)
        {
            m_SelectedTheme = theme;

            var charts = transform.GetComponentsInChildren<BaseChart>();
            foreach (var chart in charts)
            {
                chart.UpdateTheme(m_SelectedTheme);
            }
        }
    }
}
